package com.synnex.wms.controller;

import com.synnex.wms.pojo.Huodong;
import com.synnex.wms.pojo.Jiashu;
import com.synnex.wms.pojo.Peihu;
import com.synnex.wms.service.HuodongService;
import com.synnex.wms.service.JiashuService;
import com.synnex.wms.service.PeihuService;
import com.synnex.wms.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * PeihuController   陪护
 * */

@Controller
//spring的mvc控制器用来控制来自http的请求并响应
@RequestMapping(value = "/Peihu")
//定义请求映射，将处理前缀为/peihu的请求

//定义陪护控制类
public class PeihuController {

	//时间格式转换
	//创造一个新的simpledateformat对象，这里表示日期和时间应该以年-月-日 时:分:秒的形式显示
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


	//利用spring框架注入三个服务类，后续会自动进行匹配注入
	@Autowired
	private PeihuService PeihuService;
	@Autowired
	private JiashuService JiashuService;
	@Autowired
	private HuodongService HuodongService;


	//页码数设置
	 @RequestMapping(value = "/page")
	 public void page(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageIndex = 1;//设置初始的当前页，页面显示的都是第一页
        int pageSize = 4;//设置每一页显示几条数据,用于计算总页数，此处设置的值必须与sql语句的limit最后的数值一样

        PageUtil<Peihu> pageUtil = new PageUtil<Peihu>();//初始化工具类
        List<Peihu> list = new ArrayList<Peihu>();
        if (request.getParameter("pageIndex") != null) {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        }//对页面上的分页标签传的值,进行获取,也是就点击'上一页或者下一页'传过来的pageindex
        pageUtil.setPageIndex(pageIndex);//保存至工具类
        
        String jsname = request.getParameter("jsname");
        Peihu Peihu = new Peihu();
        Peihu.setJsname(jsname);
        
        int number = PeihuService.pagecount(Peihu);//调用service层方法计算出总数据量，就是多少条数据.
        pageUtil.setPageNumber(number);//保存至工具类
        pageUtil.setPageSize(pageSize);//保存至工具类
        pageUtil.setPageCount((int) Math.ceil((double) (pageUtil
                .getPageNumber() / pageUtil.getPageSize())) + 1);//计算出总页数,并封装到工具类
        int index = (pageIndex - 1) * pageSize;//计算出每一页从数据库中第几条数据开始取值，也就是limit后面的第一个数字
        Peihu.setFenye(index);
        list = PeihuService.showlist(Peihu);//调用service层的方法，取得数据库中的值
        pageUtil.setList(list);//保存到工具类中的集合
        request.setAttribute("pageUtil", pageUtil);//传递到页面，存入值栈中
		request.getRequestDispatcher("/jsp/Peihu/list.jsp").forward(request,
				response);
	}
	 
	 @RequestMapping(value = "/page2/{id}")
	 public void page2(@PathVariable Integer id,
			 HttpServletRequest request, HttpServletResponse response)
			 throws ServletException, IOException {
		 int pageIndex = 1;
		 int pageSize = 4;
		 PageUtil<Peihu> pageUtil = new PageUtil<Peihu>();
		 List<Peihu> list = new ArrayList<Peihu>();
		 if (request.getParameter("pageIndex") != null) {
			 pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		 }
		 pageUtil.setPageIndex(pageIndex);
		 
		 String jsname = request.getParameter("jsname");
	     Peihu Peihu = new Peihu();
	     Peihu.setJsname(jsname);
		 Jiashu user = JiashuService.selectByPrimaryKey(id);
		 Peihu.setJsid(id);
		 
		 int number = PeihuService.pagecount(Peihu);//调用service层方法计算出总数据量，就是多少条数据.
		 pageUtil.setPageNumber(number);//保存至工具类
		 pageUtil.setPageSize(pageSize);//保存至工具类
		 pageUtil.setPageCount((int) Math.ceil((double) (pageUtil
				 .getPageNumber() / pageUtil.getPageSize())) + 1);//计算出总页数,并封装到工具类
		 int index = (pageIndex - 1) * pageSize;//计算出每一页从数据库中第几条数据开始取值，也就是limit后面的第一个数字
		 Peihu.setFenye(index);
		 list = PeihuService.showlist(Peihu);//调用service层的方法，取得数据库中的值
		 pageUtil.setList(list);//保存到工具类中的集合
		 request.setAttribute("pageUtil", pageUtil);//传递到页面，存入值栈中
		 request.setAttribute("User", user);//传递到页面，存入值栈中
		 request.getRequestDispatcher("/jsp/Peihu/list2.jsp").forward(request,
				 response);
	 }
	 
	
	//删除
	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable Integer id) {
		Peihu ts = PeihuService.selectByPrimaryKey(id);
		PeihuService.deleteByPrimaryKey(id);
		return "redirect:/Peihu/page2/"+ts.getJsid();
	
	}
	//添加
	@RequestMapping(value = "/insert/{jsid}/{hdid}")
	public String insert(@PathVariable("jsid") Integer jsid,
			@PathVariable("hdid") Integer hdid,HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Jiashu jiashu = JiashuService.selectByPrimaryKey(jsid);
		Huodong huodong = HuodongService.selectByPrimaryKey(hdid);
		
		Peihu Peihu = new Peihu();
		Date currentTime = new Date();
		String csrq = formatter.format(currentTime);
		
		Peihu.setJsid(jsid);
		Peihu.setJsname(jiashu.getRealname());
		Peihu.setHdid(hdid);
		Peihu.setHdname(huodong.getName());
		Peihu.setEnddate(csrq);
		
		ArrayList<Peihu> peihus = new ArrayList<Peihu>();
		peihus = (ArrayList<com.synnex.wms.pojo.Peihu>) PeihuService.selectBy();
		int i = 0;
		for(Peihu peihu : peihus){
			if(peihu.getJsid()==jsid&&peihu.getHdid()==hdid){
				i++;
			}
		}
		if(i==0){
			PeihuService.insert(Peihu);
		}else{
			JOptionPane.showMessageDialog(null,"该活动您已经预约！！！");
		}
		
		return "redirect:/Peihu/page2/"+Peihu.getJsid();
	}
	

}
